package com.shoppingmall.service.impl;

import com.shoppingmall.mapper.OrderDescMapper;
import com.shoppingmall.mapper.OrdersMapper;
import com.shoppingmall.model.Car;
import com.shoppingmall.model.OrderDesc;
import com.shoppingmall.model.Orders;
import com.shoppingmall.model.view.OrderDescView;
import com.shoppingmall.service.CarService;
import com.shoppingmall.service.OrderService;
import com.shoppingmall.utils.GenarateOrderId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    private CarService carService;
    private OrdersMapper ordersMapper;
    private OrderDescMapper orderDescMapper;
    @Autowired
    private  OrderServiceImpl(CarService carService, OrdersMapper ordersMapper, OrderDescMapper orderDescMapper) {
        this.carService = carService;
        this.ordersMapper = ordersMapper;
        this.orderDescMapper = orderDescMapper;
    }

    @Override
    public void addOrder(Orders orders, List<OrderDesc> orderDescList) {
        orders.setOrder_id(GenarateOrderId.getOrderId());
        for (OrderDesc orderDesc:orderDescList){
            orderDesc.setOrder_id(orders.getOrder_id());
            Car car=new Car();
            car.setUser_id(orders.getUser_id());
            car.setGoods_id(orderDesc.getGoods_id());
            orderDesc.setCount((Integer)carService.getCarCount(car));
        }
        ordersMapper.addOrder(orders);
        orderDescMapper.addOrderDesc(orderDescList);
        for (OrderDesc orderDesc:orderDescList){
            Car car=new Car();
            car.setUser_id(orders.getUser_id());
            car.setGoods_id(orderDesc.getGoods_id());
            carService.delCar(car);
        }
    }

    @Override
    public List<Orders> getOrders(int user_id) {
        return ordersMapper.getOrders(user_id);
    }

    @Override
    public List<OrderDescView> getOrderDesc(String order_id) {
        return orderDescMapper.getOrderDesc(order_id);
    }

    @Override
    public List<Orders> getOrderById(String order_to) {
        return ordersMapper.getOrderById(order_to);
    }
}
